package com.sky.service.impl;

import com.sky.anno.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void insert(DishDTO dishDTO) {

        Dish dish = Dish.builder()
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .description(dishDTO.getDescription())
                .status(dishDTO.getStatus())
                .build();
        //将dish中的数据插入到数据库中，并实现主键回带
        dishMapper.insert(dish);

        Long dishId = dish.getId();
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if(dishFlavors != null && dishFlavors.size() > 0) {
            //将dishFlavors中的id设置为Dish的id
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dishId);
            }
            //插入dis_favor表中
            dishFlavorMapper.insert(dishFlavors);
        }
    }

    @Override
    public PageResult selectByPage(DishPageQueryDTO dishPageQueryDTO) {
        Long count = dishMapper.count(dishPageQueryDTO);

        Integer start = (dishPageQueryDTO.getPage() - 1) * dishPageQueryDTO.getPageSize();

        List<DishVO> dishList = dishMapper.selectByPage(dishPageQueryDTO, start);

        PageResult pageResult = new PageResult(count, dishList);

        return pageResult;

    }

    @Override
    public void updateStatus(Integer status, Long id) {
            Dish dish = Dish.builder()
                    .id(id)
                    .status(status)
                    .build();
            dishMapper.update(dish);
    }

    @Override
    public DishVO selectById(Long id) {
        DishVO dishVO = dishMapper.selectById(id);
        Long dishId = dishVO.getId();
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(dishId);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public List<DishVO> selectByCategoryId(Long categoryid) {
        List<DishVO> dishVO = dishMapper.selectByCategoryId(categoryid);
        return dishVO;
    }

    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish =new Dish();

        // 将dishDTO中的数据复制到dish中
        BeanUtils.copyProperties(dishDTO, dish);

        // 修改菜品
        dishMapper.update(dish);

        List<Long> dishId=new ArrayList<>();
        dishId.add(dish.getId());

        //删除菜品口味数据
        dishFlavorMapper.deleteByDishId(dishId);

        // 添加新的菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insert(flavors);
        }
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //如果菜品正在起售，则不能删除
        List<Integer> statusList = dishMapper.selectStatusByIds(ids);
        if(statusList != null && statusList.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //如果菜品与套餐关联，则不能删除
        List< Integer> categortIdList =setmealDishMapper.selectCategortByIds(ids);
        if(categortIdList != null && categortIdList.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品
        dishMapper.delete(ids);
        //删除菜品对应的口味数据
        dishFlavorMapper.deleteByDishId(ids);
    }
}
